<?php

namespace teach\controllers;

use backend\models\MallAssembleProduct;
use backend\models\MallAssembleSpecs;
use teach\components\WeChat;
use teach\components\WeChatRefund;
use teach\service\SendWechatMessage;
use teach\service\Shops;
use teach\service\StarModelAssemble;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

/**
 * 拼团商家端
 * Class ShopsController
 * @package teach\controllers
 */
class ShopsController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 发布拼团产品
     * @return \yii\web\Response
     */
    public function actionRelease()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid'); //商家id
        $title = $request->post('title'); //商品标题
        $img = $request->post('img');// 商品封面图
        $imgs = $request->post('imgs');//商品轮播展示
        //$alone_price = $request->post('alone_price');//单独购买价格
        //$assemble_price = $request->post('assemble_price');//拼团价格
        $freight = $request->post('freight');//物流价格
        $label = $request->post('label');//
        $sku = $request->post('sku');//商品sku属性
        $group_num = $request->post('group_num');//拼团人数
        $old_price = $request->post('old_price');//原价

        $detail = $request->post('detail');//底部详情编辑图
        $detail_text = $request->post('detail_text');

        $start_time = $request->post('start_time');//拼团开始时间
        $end_time = $request->post('end_time'); //拼团结束时间
        $effective_time = $request->post('effective_time');//拼团有效时间 单位秒
        $limited = $request->post('limited');//每次限购数
        $sub = $request->post('sub');//是否参与分佣 -1 不分佣 1分佣

        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = 'mid 商家id不能为空';
        } elseif (!$title) {
            $msg = 'title 标题不能为空';
        } elseif (!$img) {
            $msg = 'img 封面图不能为空';
        } elseif (!$imgs) {
            $msg = 'imgs 展示图不能为空';
        } elseif (empty(isset($freight))) {
            $msg = 'freight 邮费不能为空';
        } elseif (!$label) {
            $msg = 'label 不能为空';
        } elseif (!$sku) {
            $msg = 'sku 属性不能为空';
        } elseif (!$old_price) {
            $msg = 'old_price 原价不能为空';
        } elseif (!$group_num) {
            $msg = 'group_num 拼团人数设置不能为空';
        } elseif (!$start_time) {
            $msg = 'start_time 拼团开始使时间不能为空';
        } elseif (!$end_time) {
            $msg = 'end_time 拼团结束时间不能为空';
        } elseif (!$effective_time) {
            $msg = 'effective_time 拼团有效时间不能为空';
        } elseif (!$limited) {
            $msg = 'limited 不能为空';
        } elseif (!$sub) {
            $msg = 'sub 是否分佣不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $shops = new Shops();
            $parameter = json_decode($sku, true);

            $count_sale = $request->post('count_sale');
            $count_sale = $count_sale ? $count_sale : 0;

            $model = new MallAssembleProduct();
            $time = time();
            $model->mid = $mid;
            $model->title = $title;
            $model->img = $img;
            $model->imgs = $imgs;
            $model->detail = $detail;
            $model->detail_text = $detail_text;
            $model->freight = $freight;
            $model->count_sale = $count_sale;
            $model->group_num = $group_num;
            $model->start_time = $start_time;
            $model->end_time = $end_time;
            $model->effective_time = $effective_time;
            $model->limited = $limited;
            $model->update_time = $time;
            $model->create_time = $time;
            $model->label = $label;
            $model->status = 1;
            $model->price = $parameter[0]['assemble_price'];
            $model->sort = 100;
            $model->product_no = "pa." . $shops->createNoncestr(20) . $time;
            $model->old_price = $old_price;
            $model->sub = $sub;//此次是否参与分佣是否参与分佣

            if ($model->save()) {
                $map = [];
                foreach ($parameter as $item) {
                    $map[] = [
                        'pid' => $model->id,
                        'value' => $item['value'],
                        'stock' => $item['stock'],
                        'alone_price' => $item['alone_price'],
                        'assemble_price' => $item['assemble_price'],
                        'product_no' => 'sku.' . $shops->createNoncestr() . $time,
                        'img' => $item['img'],
                        'create_time' => $time,
                        'status' => 1,
                    ];
                }
                $res = $shops->skuBatch($map);
                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '发布成功', $model->id);
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '属性添加失败,请再次添加');
                }
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '商品添加失败');
            }


        }
        return $this->asJson($jsonData);

    }

    /**
     * 编辑商品
     * @return \yii\web\Response
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $pid = $request->post('pid');
        $mid = $request->post('mid');

        $title = $request->post('title'); //商品标题
        $img = $request->post('img');// 商品封面图
        $imgs = $request->post('imgs');//商品轮播展示
        //$alone_price = $request->post('alone_price');//单独购买价格
        //$assemble_price = $request->post('assemble_price');//拼团价格
        $freight = $request->post('freight');//物流价格
        $label = $request->post('label');//
        $sku = $request->post('sku');//商品sku属性
        $group_num = $request->post('group_num');//拼团人数
        $old_price = $request->post('old_price');//原价

        $detail = $request->post('detail');//底部详情编辑图
        $detail_text = $request->post('detail_text');

        $start_time = $request->post('start_time');//拼团开始时间
        $end_time = $request->post('end_time'); //拼团结束时间
        $effective_time = $request->post('effective_time');//拼团有效时间 单位秒
        $limited = $request->post('limited');//每次限购数
        $sub = $request->post('sub');//是否参与分佣 -1 不分佣 1分佣
        $count_sale  = $request->post('count_sale');

        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = 'mid 商家id不能为空';
        } elseif (!$pid) {
            $msg = 'pid 产品id不能为空';
        } elseif (!$title) {
            $msg = 'title 标题不能为空';
        } elseif (!$img) {
            $msg = 'img 封面图不能为空';
        } elseif (!$imgs) {
            $msg = 'imgs 展示图不能为空';
        } elseif (empty(isset($freight))) {
            $msg = 'freight 邮费不能为空';
        } elseif (empty(isset($count_sale))) {
            $msg = 'count_sale 已拼团人数不能为空';
        } elseif (!$label) {
            $msg = 'label 不能为空';
        } elseif (!$sku) {
            $msg = 'sku 属性不能为空';
        } elseif (!$old_price) {
            $msg = 'old_price 原价不能为空';
        } elseif (!$group_num) {
            $msg = 'group_num 拼团人数设置不能为空';
        } elseif (!$start_time) {
            $msg = 'start_time 拼团开始使时间不能为空';
        } elseif (!$end_time) {
            $msg = 'end_time 拼团结束时间不能为空';
        } elseif (!$effective_time) {
            $msg = 'effective_time 拼团有效时间不能为空';
        } elseif (!$limited) {
            $msg = 'limited 不能为空';
        } elseif (!$sub) {
            $msg = 'sub 是否分佣不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $model = MallAssembleProduct::find()->where(['id' => $pid, 'mid' => $mid])->one();
            if (null == $model) {
                $jsonData = ResponseHelper::buildJsonData(400, '产品信息不存在');
                return $this->asJson($jsonData);
            }

            $shops = new Shops();
            $parameter = json_decode($sku, true);

            $time = time();
            $model->title = $title;
            $model->img = $img;
            $model->imgs = $imgs;
            $model->detail = $detail;
            $model->detail_text = $detail_text;
            $model->freight = $freight;
            $model->count_sale = $count_sale;
            $model->group_num = $group_num;
            $model->start_time = $start_time;
            $model->end_time = $end_time;
            $model->effective_time = $effective_time;
            $model->limited = $limited;
            $model->update_time = $time;
            $model->label = $label;
            $model->status = 1;
            $model->price = $parameter[0]['assemble_price'];
            $model->sort = 100;
            $model->old_price = $old_price;
            $model->sub = $sub;//此次是否参与分佣是否参与分佣


            if ($model->save()) {
                $map = [];

                MallAssembleSpecs::updateAll(['status'=> -1],['pid' => $pid]);
                foreach ($parameter as $item) {
                    $map[] = [
                        'pid' => $model->id,
                        'value' => $item['value'],
                        'stock' => $item['stock'],
                        'alone_price' => $item['alone_price'],
                        'assemble_price' => $item['assemble_price'],
                        'product_no' => 'sku.' . $shops->createNoncestr() . $time,
                        'img' => $item['img'],
                        'create_time' => $time,
                        'status' => 1,
                    ];
                }
                $res = $shops->skuBatch($map);
                if ($res) {
                    $jsonData = ResponseHelper::buildJsonData(200, '拼团产品修改成功', $pid);
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '属性修改失败,请再次添加');
                }
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '产品修改失败');
            }
        }
        return $this->asJson($jsonData);
    }


    /**
     * 商家订单
     * @return \yii\web\Response
     */
    public function actionOrders()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');
        //state = 1 取 status  = -3，-2，-1，1，2，3 全部订单
        //state = 2 取 status = 1 已支付
        //state = 3  取 statues = 2 已发货
        //state = 4 取 status = 3  已完成 --》改成 status = 3 or status = -3
        //state = 5  取 status = -2 申请退款
        //state = 6  待分享 取 status = 1 而且 是在拼单时间允许的范围内
        $state = $request->post('state');//状态 -5退款删除 -4未支付删除 -3退款成功 -2 申请退款 -1 未支付 1 支付 2已发货 3已收货 4收货删除


        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = '商家id不能为空';
        } elseif (!$state) {
            $msg = '状态不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $page = $request->post('page');
            $page = $page ? $page : 1;

            if (!in_array($state,[1,2,3,4,5,6])) {
                $jsonData = ResponseHelper::buildJsonData(400, '查询状态有误');
                return $this->asJson($jsonData);
            }
            $shops = new Shops();
            $where = " mid = {$mid} ";

            switch ($state)
            {
                case 1:
                    $where .= " AND (status = -3 OR status = -2 OR status = -1 OR status = 1 OR status = 2 OR status = 3)";
                    break;
                case 2:
                    $where .= " AND status = 1";
                    break;
                case 3:
                    $where .= " AND status = 2";
                    break;
                case 4:
                    $where .= " AND (status = 3 OR status = -3) ";
                    break;
                case 5:
                    $where .= " AND status = -2 ";
                    break;
                case 6:
                    $time = time();
                    $where .= " AND status = 1 AND effective_time > ({$time} - create_time) "; //TODO  用户订单在有效时间内
                    break;
                default:
                    break;
            }
            $orders = $shops->orders($where,$page, $pre_page = 10, $state);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $orders);
        }
        return $this->asJson($jsonData);

    }

    /**
     * 商家确认退款
     */
    public function actionRefund()
    {
        $request = Yii::$app->request;

        $oid = $request->post('oid');
        $mid = $request->post('mid');
        $uid = $request->post('uid');
        $is_refuse = $request->post('is_refuse'); //1通过 -1 拒绝

        $msg = '';
        $flag = false;

        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$is_refuse) {
            $msg =  'is_refuse 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            //查看订单是否存在
            $users = new Shops();
            $where = " orders.id = {$oid} AND orders.mid = {$mid}";
            $order = $users->orderOne($where);
            if (!$order) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单不存在');
                return $this->asJson($jsonData);
            }

            //订单状态是否status = -2
            if ($order['order_status'] != -2) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单状态不可更改');
                return $this->asJson($jsonData);
            }

            $SendWeChatMessage = new SendWechatMessage();
            $redis = Yii::$app->redis;
            $WeChat = new WeChat();

            //拒绝 将订单改为-6
            if ($is_refuse == -1) {
                $map = " status = -6";
                $whereFilter = " id = {$oid} AND mid = {$mid} ";
                $res = $users->orderRefundStatus($map, $whereFilter);
                if ($res) {
                    //TODO 拒绝后发小程序消息
                    while (true) {
                        $formid = $redis->rpop("formid_{$order['openid']}");
                        $status = $WeChat->sendMsg($SendWeChatMessage->refundStatus($order['openid'],$formid, $order['out_trade_no'], '拒绝退款',$order['total_fee'], '如有疑问请联系商家','请在订单管理查看详情'));
                        if ($status['errcode'] == 0 || $formid == false) {
                            break;
                        }
                    }
                    $jsonData = ResponseHelper::buildJsonData(200, '拒绝成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '拒绝失败');
                }
                return $this->asJson($jsonData);
            }

            //确认
            if ($is_refuse == 1) {
                //TODO 调起微信做退款操作 把微信操作返回信息用json存在pay中
                $appid = Yii::$app->params['serviceNum'][$mid]['appid']; //公众号appid
                $mch_id = Yii::$app->params['serviceNum']['mchId']; //服务商号
                $sub_mch_id = Yii::$app->params['serviceNum'][$mid]['sub_mch_id'];
                $key = Yii::$app->params['serviceNum']['md5Key']; //支付密匙设置
                $transaction_id = $order['transaction_id']; //微信订单号
                $out_refund_no = 'Refund' . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);; //退款订单号
                $total_fee = $order['total_fee']; //订单金额
                $refund_fee = $order['total_fee']; //申请退款金额 暂时写全额退款
                $WeChatRefund = new WeChatRefund($appid, $mch_id, $key, $sub_mch_id, $transaction_id, $out_refund_no, $total_fee, $refund_fee);
                $result = $WeChatRefund->refund();
                if ($result['return_code'] == 'SUCCESS') {
                   if ($result['result_code'] == 'SUCCESS') {
                       //退款成功
                       //TODO 确认退款 同时要把mall_assemble_pay 中status 改成已退款 -1 mall_assemble_order status = -3
                       //TODO 把 bill中的积分状态也改为已退款status = -1(out_trade_no,uid, mid)，不计入用户积分里
                       $pay_id = $order['p_id'];
                       $out_trade_no = $order['out_trade_no'];
                       $res_json = json_encode($result, true);
                       $ret = $users->orderBatchStatus($pay_id, $oid, $out_trade_no, $res_json);
                       if ($ret) {
                           //TODO 确认退款成功后 发送小程序消息
                           //$SendWeChatMessage = new SendWechatMessage();
                           //$redis = Yii::$app->redis;
                           //$WeChat = new WeChat();
                           //告诉用户退款成功
                           while (true) {
                               $formid = $redis->rpop("formid_{$order['openid']}");
                               $status = $WeChat->sendMsg($SendWeChatMessage->refundStatus($order['openid'],$formid, $order['out_trade_no'], '退款成功',$order['total_fee'], '如有疑问请联系商家','请在订单管理查看详情'));
                               if ($status['errcode'] == 0 || $formid == false) {
                                   break;
                               }
                           }
                           //$WeChat->sendMsgSome($SendWeChatMessage->refundStatus($order['openid'], $order['out_trade_no'], '退款成功',$order['total_fee'], '如有疑问请联系商家','请在订单管理查看详情'));
                           //调用成功,退款成功,记录成功
                           $jsonData = ResponseHelper::buildJsonData(200, '调用成功,退款成功,记录成功');
                           return $this->asJson($jsonData);
                       } else {
                           //调用成功,退款成功,记录失败
                           $jsonData = ResponseHelper::buildJsonData(200, '调用成功,退款成功,记录失败');
                           return $this->asJson($jsonData);
                       }

                   } else {
                       $jsonData = ResponseHelper::buildJsonData(400,'调用成功,退款失败', $result);
                       return $this->asJson($jsonData);
                   }
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '调用失败,退款失败',$result);
                    return $this->asJson($jsonData);
                }



                /**
                $map = " status = -3 ";
                $whereFilter = " id = {$oid} AND mid = {$mid} ";
                $res = $users->orderRefundStatus($map, $whereFilter);
                if ($res) {
                    //TODO 确认退款成功后 发送小程序消息
                    $jsonData = ResponseHelper::buildJsonData(200, '确认退款成功');
                } else {
                    $jsonData = ResponseHelper::buildJsonData(400, '退款失败');
                }
                return $this->asJson($jsonData);
                 * */
            }
            $jsonData = ResponseHelper::buildJsonData(400, '非法操作');
        }
        return $this->asJson($jsonData);
    }

    /**
     * 发货
     * @return \yii\web\Response
     */
    public function actionExpress()
    {
        $request = Yii::$app->request;
        $oid = $request->post('oid');
        $mid = $request->post('mid');
        $logistics = $request->post('logistics');
        $order_num = trim($request->post('order_num'));

        $msg = '';
        $flag = false;
        if (!$oid) {
            $msg = 'oid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$logistics) {
            $msg = 'logistics 不能为空';
        } elseif (!$order_num) {
            $msg = 'order_num 不能为空';
        } else {
            $flag = true;

        }

        if (!$flag) {
            $jsonData= ResponseHelper::buildJsonData(400, $msg);
        } else {
            $shops = new Shops();
            $time = time();
            $where = " mid = {$mid} AND id = {$oid}";
            $order = $shops->orderFindOne($where);

            //订单是否存在
            if (!$order) {
                $jsonData = ResponseHelper::buildJsonData(400, '订单有误');
                return $this->asJson($jsonData);
            }

            //订单是否已支付状态
            if ($order['status'] != 1) {
                $jsonData = ResponseHelper::buildJsonData(400,  '请勿非法操作');
                return $this->asJson($jsonData);
            }

            $map = " logistic = '{$logistics}', order_num = '{$order_num}', status = 2, send_time = {$time} ";

            $res = $shops->orderExpress($map, $where);
            if ($res) {
                $this->Send($order, $logistics, $order_num);
                $jsonData = ResponseHelper::buildJsonData(200, '成功');
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '失败');
            }
        }
        return $this->asJson($jsonData);
    }

    private function Send($order, $logistics, $order_num)
    {
        //$redis = Yii::$app->redis;
        $SendWeChatMessage = new SendWechatMessage();
        //告诉用户已经发货
        //$tip = "恭喜拼团成功,商家已发货,具体物流信息请打开小程序查看";

        $WeChat = new WeChat();
        //小程序模板消息
//        while (true) {
//            $formid = $redis->rpop("formid_{$order['openid']}");
//            $status = $WeChat->sendMsg($SendWeChatMessage->deliver($order['openid'], $formid, $order['out_trade_no'], $order['phone'], $order['name'], $logistics, $order_num, $tip));
//            if ($status['errcode'] == 0 || $formid == false) {
//                break;
//            }
//        }

        //小程序订阅消息
        $WeChat->send($SendWeChatMessage->delivers($order["openid"], $order["out_trade_no"], $logistics, $order_num));

        //查找商家,发送小程序消息
        $shop = new Shops();
        $andWhere = " id = {$order['mid']} ";
        $store = $shop->storeOne($andWhere);
//        while (true) {
//            $formid_store = $redis->rpop("formid_{$store['openid']}");
//            $status_store = $WeChat->sendMsg($SendWeChatMessage->deliver($store['openid'], $formid_store, $order['out_trade_no'], $order['phone'], $order['name'], $logistics, $order_num, $tip));
//            if ($status_store['errcode'] == 0 || $formid_store == false) {
//                break;
//            }
//        }

        //小程序订阅消息
        $WeChat->send($SendWeChatMessage->delivers($store["openid"], $order["out_trade_no"], $logistics, $order_num));

    }

    /**
     * 退款申请详情
     * @return \yii\web\Response
     */
    public function actionRefundDetail()
    {
        $request = Yii::$app->request;
        $oid = $request->post('oid');
        $uid = $request->post('uid');
        $mid = $request->post('mid');
        $out_trade_no = $request->post('out_trade_no');

        $msg = '';
        $flag = false;

        if (!$oid) {
            $msg = "oid 不能为空";
        } elseif (!$uid) {
            $msg = "uid 不能为空";
        } elseif (!$mid) {
            $msg = "mid 不能为空";
        } elseif (!$out_trade_no) {
            $msg = "out_trade_no 不能为空";
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $shops = new Shops();
            $where = " oid = {$oid} AND uid = {$uid} AND mid = {$mid} AND out_trade_no = '{$out_trade_no}'";
            $refund = $shops->refundOne($where);
            if ($refund) {
                $enum[1] = "1我要退款（无需退货)";
                $enum[2] = "我要退货退款";
                $refund['types'] = $enum[$refund['types']];
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $refund);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '申请不存在');
            }
        }
        return $this->asJson($jsonData);

    }

    /**
     * 商家端查看发布的拼团订单
     */
    public function actionAssembleList()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid');
        $state = $request->post('state'); //1全部产品 2正在拼团 3 未开始 4 已结束

        $msg = '';
        $flag = false;

        if (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$state) {
            $msg = 'state 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {

            $page = $request->post('page');
            $page = $page ? $page : 1;

            if (!in_array($state, [1, 2, 3,4])) {
                $jsonData = ResponseHelper::buildJsonData(400,'状态有误');
                return $this->asJson($jsonData);
            }
            $shops = new Shops();
            $where = " mid = {$mid} AND status = 1";
            $time = time();
            if ($state == 2) {
                $where .= " AND start_time < {$time} AND end_time > {$time}";
            }
            if ($state == 3) {
                $where .= " AND start_time > {$time} ";
            }
            if ($state == 4 ) {
                $where .= " AND end_time < {$time}";
            }
            $assemble = $shops->productAssembleList($where, $page, $pre_page = 10);
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $assemble);
        }
        return $this->asJson($jsonData);
    }

    public function actionDeal()
    {
        $request = Yii::$app->request;
        $products = $request->post('products');
        $mid = $request->post('mid');
        $state = $request->post('state'); //3删除

        $msg = '';
        $flag = false;
        if (!$products) {
            $msg = 'products 产品集不能为空';
        } elseif (!$state) {
            $msg = 'state 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(200, $msg);
        } else {
            $shops = new Shops();
            $pds = json_decode($products, true);
            foreach ($pds as $key => $val) {
                //看看每个产品的需要的key存在不存在
                if (!array_key_exists('pid', $val)) {
                    $jsonData = ResponseHelper::buildJsonData(400, '缺少产品key值:pid');
                    return $this->asJson($jsonData);
                }
            }

            foreach ($pds as $_key => $_val) {
                $where = " id = {$_val['pid']} AND mid = {$mid} ";
                $shops->productAssembleDelete($where);
            }
            $jsonData = ResponseHelper::buildJsonData(200, '成功');
        }
        return $this->asJson($jsonData);
    }

    /**
     * 测试分佣
     */
    public function actionDealOrder()
    {
        $StarModelAssemble = new StarModelAssemble();
        $oid = 37;
        $mid = 10000;
        $uid = 10014;
        $result = $StarModelAssemble->payAll($oid, $mid, $uid);
        var_dump($result);
    }
}